<?php
namespace Web\Controller;
use Common\Model\AdModel;
use Common\Model\CategoryModel;
use Common\Model\LinksModel;
use Common\Model\OptionsModel;
use Common\Model\VideoLinkModel;
use Information\Model\InformationModel;
use System\Model\DocumentsModel;
use Think\Controller;
class IndexController extends WebBaseController {

    private $links_model;
    private $information_model;
    private $documents_model;
    private $ad_model;
    private $category_model;
    private $video_link_model;
    private $options_model;

    public function __construct()
    {
        parent::__construct();
        $this->links_model = new LinksModel();
        $this->information_model = new InformationModel();
        $this->documents_model = new DocumentsModel();
        $this->ad_model = new AdModel();
        $this->category_model = new CategoryModel();
        $this->video_link_model = new VideoLinkModel();
        $this->options_model = new OptionsModel();
        //友情链接
        $links = $this->links_model->order('listorder')->select();//dump($links);
        $this->assign('links', $links);
        $this->assign('bottom_option', $this->options_model->where(['option_name' => 'website_options'])->find());
    }

    public function header() {
        $this->display();
    }

    public function index(){

        //拍摄花絮
        $this->assign('ph_list', $this->getInfo(9, 10000));//dump($ph_list);

        //图片新闻
        $this->assign('p_news', $this->getInfo(1, 9));//dump($p_news);

        //图片新闻推荐
        $this->assign('p_r_news', $this->getInfo(1, 1000, 1));//dump($this->getInfo(1, 1000, 1));

        //公告通知
        $this->assign('m_news', $this->getInfo(2, 3));//dump($m_news);

        //专题报道
        $this->assign($this->getPicList(3, 6));//dump($this->getPicList(3, 6));

        //反腐倡廉
        $this->assign($this->getPicList(5, 6));//dump($this->getPicList(5, 6));

        //食品安全
        $this->assign($this->getPicList(4, 6));//dump($this->getPicList(3, 6));

        //法制播报
        $this->assign($this->getPicList(6, 6));//dump($this->getPicList(3, 6));

        //社区播报
        $this->assign($this->getPicList(7, 6));//dump($this->getPicList(3, 6));

        //企业展播
        $this->assign($this->getPicList(8, 6));//dump($this->getPicList(3, 6));

        //环境报道
        $this->assign($this->getPicList(11, 6));//dump($this->getPicList(3, 6));

        //战略合作伙伴
        $this->assign('list12', $this->getInfo(12, 10000));//dump($this->getPicList(3, 6));

        //右侧视频广告位
        $this->assign('video_ads', $this->ad_model->where(['type' => 4])->find());
        $video = $this->video_link_model->order('id desc')->find();
//        $video['link_url'] = setUrl($video['link_url']);
        $this->assign('video', $video);

        //首页第二广告位
        $this->assign('second_ads', $this->ad_model->where(['type' => 1])->find());

        //首页第一广告位
        $this->assign('first_ads', $this->ad_model->where(['type' => 6])->find());

        //首页第三广告位
        $this->assign('third_ads', $this->ad_model->where(['type' => 5])->find());

        //首页右侧
        $this->assign('right_ads', $this->ad_model->where(['type' => 2])->find());

        $this->display();
    }

    public function getInfo($type, $number, $recommend=0) {
        $where['inf_type'] = $type;//dump($recommend);
        if($recommend) {
            $where['recommend'] = $recommend;
        }
        $list = $this->information_model->where($where)->order('create_time desc')->limit($number)->select();
        foreach ($list as $k => $v) {
            if(strlen($v['title']) > 40) {
                $list[$k]['title'] = mb_substr($v['title'], 0, 18).'...';

            }
            $list[$k]['cover'] = getUrl($v['cover']);
        }
        return $list;
    }

    public function getPicList($type, $number) {
        $first = $this->information_model->where(['inf_type' => $type])->order('create_time desc')->find();
        $first['cover'] = getUrl($first['cover']);
        $first['title'] = strlen($first['title']) < 40 ?  $first['title']:mb_substr($first['title'], 0, 18).'...';
        $list = $this->information_model->where(['inf_type' => $type])->order('create_time desc')->limit(1, $number)->select();
        foreach ($list as $k => $v) {
            if(strlen($v['title']) > 40) {
                $list[$k]['title'] = mb_substr($v['title'], 0, 18).'...';
            }
            $list[$k]['cover'] = getUrl($v['cover']);
        }
        $data = [
            'first'.$type => $first,
            'list'.$type  => $list,
        ];
        return $data;
    }

    /**
     * 详情
     */
    public function index_shooting() {
        $info_id = I('get.info_id');
        $this->addScanTimes($info_id);
        $info= $this->information_model->find($info_id);

        $info['create_time'] = date('Y-m-d', strtotime($info['create_time']));
        $this->assign('info', $info);//dump($info);
        $last = $this->information_model->where(['id' => ['lt', $info_id], 'inf_type' => $info['inf_type']])->order('id desc')->find();
        $next = $this->information_model->where(['id' => ['gt', $info_id], 'inf_type' => $info['inf_type']])->order('id asc')->find();
        $last = $last ? :  $this->information_model->where(['inf_type' => $info['inf_type']])->order('id desc')->find();
        $next = $next ? :  $this->information_model->where(['inf_type' => $info['inf_type']])->order('id asc')->find();


        $this->assign('last', $last);
        $this->assign('next', $next);

        $this->display();
    }

    /**
     * @param $info_id
     * 添加浏览次数
     */
    public function addScanTimes($info_id) {
        $this->information_model->where(['id' => $info_id])->setInc('scan_times');
    }

    /**
     * 更多列表
     */
    public function news() {
        //广告位
        $this->assign('left_ads', $this->ad_model->where(['type' => 3])->find());

        $inf_type = I('get.inf_type');
        //标题
        $this->assign('type_name', $this->information_model->getType($inf_type));
        //推荐
        $this->assign('recommend_list', $this->getInfo($inf_type, 3, 1));//dump($this->information_model->getLastSql());dump($this->getInfo($inf_type, 3, 1));

        $count = $this->information_model
            ->where(['inf_type' => $inf_type])
            ->count();
        $page = $this->page($count, 20);
        $result = $this->information_model
            ->where(['inf_type' => $inf_type])
            ->limit($page->firstRow, $page->listRows)
            ->order('create_time desc')
            ->select();
        foreach ($result as $k => $v) {
            if(strlen($v['title']) > 40) {
                $result[$k]['title'] = mb_substr($v['title'], 0, 18).'...';
            }
            $result[$k]['cover'] = getUrl($v['cover']);
            $result[$k]['create_time'] = date('Y-m-d', strtotime($v['create_time']));
        }

        $this->assign('lists', $result);
        $this->assign('Page', $page->show());
        $this->display();
    }

    /**
     * 合作伙伴
     */
    public function partners() {
        $inf_type = 12;// I('get.inf_type');
        //标题
        $this->assign('type_name', $this->information_model->getType($inf_type));
        //推荐
        $this->assign('recommend_list', $this->getInfo($inf_type, 3, 1));//dump($this->information_model->getLastSql());dump($this->getInfo($inf_type, 3, 1));

        $count = $this->information_model
            ->where(['inf_type' => $inf_type])
            ->count();
        $page = $this->page($count, 16);
        $result = $this->information_model
            ->where(['inf_type' => $inf_type])
            ->limit($page->firstRow, $page->listRows)
            ->order('create_time desc')
            ->select();
        foreach ($result as $k => $v) {
            if(strlen($v['title']) > 40) {
                $result[$k]['title'] = mb_substr($v['title'], 0, 18).'...';
            }
            $result[$k]['cover'] = getUrl($v['cover']);
            $result[$k]['create_time'] = date('Y-m-d', strtotime($v['create_time']));
        }

        $this->assign('lists', $result);
        $this->assign('Page', $page->show());
        $this->display();
    }

    /**
     * 文档页面
     */
    public function getDoc() {
        $type = I('get.type');
        $info = $this->documents_model->where(['doc_class' => $type])->find();
        $this->assign('info', $info);
        $this->display();
    }

    public function video() {
        //广告位
        $this->assign('left_ads', $this->ad_model->where(['type' => 3])->find());

        $inf_type = '1';// I('get.inf_type');
        //标题
        $this->assign('type_name', $this->information_model->getType($inf_type));
        //推荐
        $this->assign('recommend_list', $this->getInfo($inf_type, 3, 1));

        $cate_id = I('get.cate_id');
        if(!$cate_id) {
            $cate = $this->category_model->order('id asc')->find();
            $cate_id = $cate['id'];
        }else {
            $cate = $this->category_model->find($cate_id);
        }
        $where['cate_id'] = $cate_id;

        $count = $this->video_link_model
            ->where($where)
            ->count();
        $page = $this->page($count, 20);
        $result = $this->video_link_model
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->select();


        foreach ($result as $k => $v) {
            $result[$k]['url'] = $v['link_url'] ? : U('Web/Index/video_details', ['id' => $v['id']]);
            $result[$k]['cover'] = getUrl($v['cover']);
            $result[$k]['update_time'] = date('Y-m-d', strtotime($v['update_time']));
        }
        $this->assign('lists', $result);
        $this->assign('cate_list', $this->category_model->select());
        $this->assign('cate_name', $cate['name']);
        $this->display();
    }

    public function video_details() {



        $info_id = I('get.id');
        $this->addScanTimesVideo($info_id);
        $info= $this->video_link_model->find($info_id);

        $info['create_time'] = date('Y-m-d', strtotime($info['update_time']));
        $this->assign('info', $info);//dump($info);
        $last = $this->video_link_model->where(['id' => ['lt', $info_id], 'cate_id' => $info['cate_id']])->order('id desc')->find();
        $next = $this->video_link_model->where(['id' => ['gt', $info_id], 'cate_id' => $info['cate_id']])->order('id asc')->find();
        $last = $last ? :  $this->video_link_model->where(['cate_id' => $info['cate_id']])->order('id desc')->find();
        $next = $next ? :  $this->video_link_model->where(['cate_id' => $info['cate_id']])->order('id asc')->find();


        $this->assign('last', $last);
        $this->assign('next', $next);

        $this->display();
    }

    public function addScanTimesVideo($info_id) {
        $this->video_link_model->where(['id' => $info_id])->setInc('scan_times');
    }

}